package ${package.ServiceImpl};

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.tanjie.blackbean.mybatis.query.Condition;
import com.tanjie.blackbean.mybatis.query.PageParam;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.tanjie.blackbean.mybatis.support.PageConverter;
import ${package.Entity}.${entity};
import ${package.Entity}.${entity}DTO;
import ${package.Entity}.${entity}VO;
import ${package.Entity}.${entity}Query;
import ${package.Mapper}.${table.mapperName};
import ${package.Service}.${table.serviceName};
import ${package.Service}.${entity}DaoService;
import ${superServiceImplClassPackage};
import com.tanjie.blackbean.pojo.exception.BizException;
import com.tanjie.blackbean.pojo.response.BizErrorCode;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * ${table.comment!}
 *
 * @author ${author}
 * @since ${date}
 */
@Service
public class ${table.serviceImplName} implements ${table.serviceName} {

    @Resource
    ${table.mapperName} ${table.mapperName?uncap_first};

    @Resource
    ${entity}DaoService ${entity?uncap_first}Service;

    @Override
    public void create(${entity}DTO dto) {
      ${entity} createOne = BeanUtil.copyProperties(dto, ${entity}.class);
      ${table.mapperName?uncap_first}.insert(createOne);
    }

    @Override
    public void update(${entity}DTO dto) {
        String id = dto.getId();
        if (StrUtil.isBlank(id)) {
            throw new BizException(BizErrorCode.ID_REQUIRE_NOT_BLANK);
        }
        ${entity} updateOne = BeanUtil.copyProperties(dto, ${entity}.class);
        ${entity?uncap_first}Mapper.updateById(updateOne);
    }

    @Override
    public void delete(String id) {
        if (StrUtil.isBlank(id)) {
            throw new BizException(BizErrorCode.ID_REQUIRE_NOT_BLANK);
        }
        ${entity} existOne = ${entity?uncap_first}Mapper.selectById(id);
        if (existOne == null) {
            throw new BizException(BizErrorCode.ID_NOT_EXIST);
        }
        ${entity?uncap_first}Mapper.deleteById(id);
    }

    @Override
    public ${entity}VO detail(String id) {
        if (StrUtil.isBlank(id)) {
            throw new BizException(BizErrorCode.ID_REQUIRE_NOT_BLANK);
        }
        ${entity} existOne = ${entity?uncap_first}Mapper.selectById(id);
        if (existOne == null) {
            throw new BizException(BizErrorCode.ID_NOT_EXIST);
        }
        return BeanUtil.copyProperties(existOne, ${entity}VO.class);
    }

    @Override
    public List<${entity}VO> queryList(${entity}Query dto) {
        QueryWrapper<${entity}> queryWrapper = getQueryWrapper(dto);
        List<${entity}> list = ${entity?uncap_first}Mapper.selectList(queryWrapper);
        return BeanUtil.copyToList(list, ${entity}VO.class);
    }

    @Override
    public IPage<${entity}VO> queryPage(${entity}Query dto, PageParam pageParam) {
        IPage<${entity}> page = Condition.getPage(pageParam);
        QueryWrapper<${entity}> queryWrapper = getQueryWrapper(dto);
        return PageConverter.convert(${entity?uncap_first}Mapper.selectPage(page, queryWrapper),
            ${entity?uncap_first} -> BeanUtil.copyProperties(${entity?uncap_first}, ${entity}VO.class));
    }

    private QueryWrapper<${entity}> getQueryWrapper(${entity}Query dto) {
        QueryWrapper<${entity}> queryWrapper = null;
        if (dto != null) {
            queryWrapper = Condition.getQueryWrapper(BeanUtil.copyProperties(dto, ${entity}.class));
        }
        return queryWrapper;
    }

}
